package com.itbour.service.yjx.trade.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.yjx.trade.YjxPriceInfoDao;
import com.itbour.dto.video.YjxPriceInfoDto;
import com.itbour.service.yjx.trade.YjxPriceInfoService;

/**
 * 一键秀 价格套餐 
 * @author LIQI/ Kris
 */
public class YjxPriceInfoServiceImpl implements YjxPriceInfoService {
	
	private YjxPriceInfoDao yjxPriceInfoDao;

	public Page getPriceInfo(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxPriceInfoDao.getPriceInfoCnt(parm);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<YjxPriceInfoDto> data = yjxPriceInfoDao.getPriceInfo(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

	public boolean editPriceInfo(YjxPriceInfoDto yjxPriceInfoDto) {
		if (yjxPriceInfoDto.getPrice_type() == null){
			return false;
		}
		if (yjxPriceInfoDto.getShow_name() == null){
			return false;
		}
		if (yjxPriceInfoDto.getPricing() == null){
			return false;
		}
		if (yjxPriceInfoDto.getUnit() == null){
			return false;
		}
		if (yjxPriceInfoDto.getState() == null){
			return false;
		}
		Map<String, Object> parm =new HashMap<String, Object>();
		parm.put("price_type", yjxPriceInfoDto.getPrice_type());
		parm.put("show_name", yjxPriceInfoDto.getShow_name());
		parm.put("name", yjxPriceInfoDto.getName());
		parm.put("pricing", yjxPriceInfoDto.getPricing());
		parm.put("unit_price", yjxPriceInfoDto.getUnit_price());
		parm.put("prices", yjxPriceInfoDto.getPrices());
		parm.put("unit", yjxPriceInfoDto.getUnit());
		parm.put("state", yjxPriceInfoDto.getState());
		parm.put("desc", yjxPriceInfoDto.getDesc());
		try {
			if (yjxPriceInfoDto.getPrice_id()==null){
				parm.put("update_by","add:"+yjxPriceInfoDto.getUpdate_by());
				yjxPriceInfoDao.insertPriceInfo(parm);// 新建套餐
			} else {
				parm.put("update_by","upd:"+yjxPriceInfoDto.getUpdate_by());
				if (yjxPriceInfoDto.getOrder()!=null){
					parm.put("order", yjxPriceInfoDto.getOrder());
				}
				parm.put("price_id", yjxPriceInfoDto.getPrice_id());
				yjxPriceInfoDao.updPriceInfo(parm);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean dePriceInfo(Map<String, Object> parm) {
		try {
			yjxPriceInfoDao.delPriceInfo(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	
	
	
	public YjxPriceInfoDao getYjxPriceInfoDao() {
		return yjxPriceInfoDao;
	}

	public void setYjxPriceInfoDao(YjxPriceInfoDao yjxPriceInfoDao) {
		this.yjxPriceInfoDao = yjxPriceInfoDao;
	}

	public List<YjxPriceInfoDto> getOnePriceInfo(Map<String, Object> parm) {
		return yjxPriceInfoDao.getPriceInfo(parm);
	}

	@Override
	public boolean updPriceOrder(Map<String, Object> parm) {
		try {
			yjxPriceInfoDao.updPriceInfo(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
